<?php
require('../includes/functions.inc.php');
require('../includes/LITTLE_ConsignmentExcelReader.class.php');

ini_set( "memory_limit" , "128M");
ini_set("max_execution_time",0);

$smarty = SmartyManager::getSmarty();

/*************************************************************
 * 每个上传文件只要修改下面这些变量就基本可以了
 ************************************************************/
$formFilename = 'consignmentFile';

//不可缺少的列
$obligatoCols = array(
	'产品质量证明书号','产品质量证明书号','发车证书号','订单号','订单子项号','购单号','发货批次'
);

//电子表格和数据库表中的列的对应关系
$tableCols = array(
		'产品质量证明书号' 	=> 	'certificationNumber',
		'产品质量证明书编号' 	=> 	'certificationCodification',
		'发车证书号'	=> 'facheCertificationNumber',
		'订单号'	=>	'orderNumber',
		'订单子项号' 	=> 	'orderSubitemNumber',
		'购单号' 		=> 	'purchaseNumber',
		'发货批次'	=>	'consignmentBatch'
);

//对比时用来比较的列
$compareCols = array();
$record = Filter::forDBInsertion($_POST['record'], true);

/************************************************************
 * 文件上传
 ***********************************************************/
try{
	//$file 在本程序中用作了全局变量，用来在需要filename的函数中使用
	$file = saveUploadedFile($formFilename);
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	exit();
}

try{
	beginTransaction();
	
	//然后read上传的表格的信息，将其中的数据存入数据库中
	$littleReader = new LITTLE_ConsignmentExcelReader();
	$littleReader->read($file->getNewPath());

	$littleReader->obligatoColumns = $obligatoCols;
	$littleReader->tableColumns = $tableCols;
	//$littleReader->compareColumns = $compareCols;

	$warnMsg = "";
	try{
		$littleReader->checkMyFirstRow();
	}catch(UnknownColumnIndex $e){
		//这个函数可能抛出两个异常，一个是LackSomeColumn
		//另一个是UnknownColumnIndex。
		//对于UnknownColumnIndex只需要一个警告提醒，而不会像LackSomeColumn一样停止运行
		$warnMsg .= $e->getMessage();
	}
	
	$littleReader->checkConsignmentUploadColumn();

	if($date = $littleReader->myHaveUploaded()){
		throw new UploadSameFileException($file->getFilename(), $date);
	}else {
		$littleReader->saveFileInfo($record);
	}
	//这个放在saveFileInfo下面是因为如果放在上边的话，保存file信息后获得不到inser_id。

	$littleReader->myReadIntoDB();
	
	LogInserter::logForInsert("导入发货批次文件：{$file->getFilename()}");
	
	$consignmentArray = $littleReader->getConsignmentColumn();
	
	foreach ($consignmentArray as $value) {
		$certificationNumber = $value[0];
		$certificationCodification = $value[1];
		$facheNumber = $value[2];
		$orderNumber = $value[3];
		$orderSubitemNumber = $value[4];
		$purchaseNumber = $value[5];
		$consignmentBatch = $value[6];
		
		if($consignmentBatch)
			$sql2 = "update little_main set consignmentBatch = '$consignmentBatch' where ";
		else 
			$sql2 = "update little_main set consignmentBatch = NULL where ";
		$wheres = array();
		if($certificationNumber){
			array_push($wheres, "certificationNumber = '{$certificationNumber}'");
		}
		if($certificationCodification){
			array_push($wheres, "certificationCodification = '{$certificationCodification}'");
		}
		if($facheNumber){
			array_push($wheres, "facheNumber = '{$facheNumber}'");
		}
		if($orderNumber){
			array_push($wheres, "orderNumber = '{$orderNumber}'");
		}
		if($orderSubitemNumber){
			array_push($wheres, "orderSubitemNumber = '{$orderSubitemNumber}'");
		}
		if($purchaseNumber){
			array_push($wheres, "purchaseNumber = '{$purchaseNumber}'");
		}
		$where = join(' and ',$wheres);
		$sql2 .= $where;
		DB::query($sql2);
		
		if($certificationNumber){
			if($consignmentBatch)
				$sql3 = "update little_ruku set consignmentBatch = '$consignmentBatch' where certificationNumber = '{$certificationNumber}'";
			else 
				$sql3 = "update little_ruku set consignmentBatch = NULL where certificationNumber = '{$certificationNumber}'";
			DB::query($sql3);
			
			if($consignmentBatch)
				$sql4 = "update little_chuku set consignmentBatch = '$consignmentBatch' where certificationNumber = '{$certificationNumber}'";
			else 
				$sql4 = "update little_chuku set consignmentBatch = NULL where certificationNumber = '{$certificationNumber}'";
			DB::query($sql4);	
		}
	}
}catch(DBQueryException $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errorType', 'consignmentUpload');
	$smarty->assign("errTitle", '上传发货批次文件失败1');
	$smarty->display('error.html');
	
	rollback();
	exit();
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errorType', 'consignmentUpload');
	$smarty->assign("errTitle", '上传发货批次文件失败2');
	$smarty->display('error.html');
	rollback();
	exit();
}

$smarty->assign('successMsg', $file->getFilename().'上传成功');
$smarty->assign("successTitle", '上传发货批次文件成功');
$smarty->assign('successType', 'consignmentUpload');
if(isset($warnMsg)){
	$smarty->assign('warnMsg', $warnMsg);
}
$smarty->display('success.html');
commit();
?>